package com.biz.primus.app.rest.controller.depot;

import com.biz.primus.app.rest.controller.base.RestBaseController;
import com.biz.primus.app.rest.feign.DepotFeignClient;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.base.global.GlobalParams;
import com.biz.primus.model.depot.exception.DepotException;
import com.biz.primus.model.depot.vo.DepotRespVo;
import com.biz.primus.model.depot.vo.FreightTemplateDetailRespVo;
import com.biz.primus.model.depot.vo.SearchDepotConditionVo;
import com.biz.primus.model.depot.vo.SearchDepotResVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * DepotController
 *
 * @author JKLiues
 * @date 2017年11月14日
 */
@RestController
@RequestMapping("depot")
public class DepotController extends RestBaseController {

    private final DepotFeignClient depotFeignClient;

    @Autowired
    public DepotController(DepotFeignClient depotFeignClient) {
        this.depotFeignClient = depotFeignClient;
    }

    @ApiOperation(value = "根据门店编码查门店", response = DepotRespVo.class)
    @GetMapping("findDepot")
    public JsonResult findDepot(@ApiParam(value = "门店编码") @RequestParam("depotCode") String depotCode) {
        Optional<DepotRespVo> depotOption = depotFeignClient.getByDepotCodeFRdb(depotCode);
        return depotOption.map(JsonResult::new).orElseGet(() -> new JsonResult(DepotException.DEPOT_NOT_EXIT));
    }

    @ApiOperation(value = "搜索最近门店", response = SearchDepotResVo.class)
    @PostMapping("searchNearbyDepot")
    public JsonResult searchNearbyDepot(GlobalParams gp, @RequestBody SearchDepotConditionVo vo) {
        super.buildRequest(gp,vo);
        SearchDepotResVo searchDepotResVo = depotFeignClient.searchNearbyDepot(vo);
        return new JsonResult(searchDepotResVo);
    }

    @ApiOperation(value = "搜索附近门店", responseContainer = "List", response = DepotRespVo.class)
    @PostMapping("searchNearDepot")
    public JsonResult findNearDepots(GlobalParams gp,@RequestBody SearchDepotConditionVo vo) {
        super.buildRequest(gp,vo);
        List<DepotRespVo> nearDepots = depotFeignClient.findNearDepots(vo);
        return new JsonResult(nearDepots);
    }

    @ApiOperation(value = "获取运费策略", response = FreightTemplateDetailRespVo.class, responseContainer = "List")
    @GetMapping("getFreights")
    public JsonResult getAllAvailableFreight() {
        List<FreightTemplateDetailRespVo> allAvailableFreight = depotFeignClient.getAllAvailableFreight();
        return new JsonResult(allAvailableFreight);
    }

}